package ljl.alg.jianzhioffer.round2;

import java.util.HashSet;

public class _57_two_sum {

    /**
     * 没看清递增排序，应该用双指针的
     * */
    public int[] twoSum2(int[] nums, int target) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (set.contains(target - num))
                return new int[]{num, target - num};
            set.add(num);
        }
        return null;
    }
    
    /**
     * 双指针写错这么多次我是没想到的
     * */
    public int[] twoSum(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l < r) {
            int sum = nums[l] + nums[r];
            if (sum == target) {
                return new int[] {nums[l], nums[r]};
            }
            if (sum > target) r--;
            else l++;
        }
        return null;
    }
}
